package com.tosh.model.comp;

import com.tosh.model.Factor;
import com.tosh.model.Match;
import com.tosh.util.Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: arsentyev
 * Date: 08.10.12
 */
public class Marathon {
    public static List<Match> getMatches(String url) {
        url = "http://www.marathonbet.com/ru/ajaxevents.htm?" + url;
        List<Match> matches = new ArrayList<Match>();
        Match match = null;
        Document doc = null;

        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
            return getMatches(url);
        }

        Elements trs = doc.select("table.foot-market tbody tr.event-header");
        int i = 0;
        for (Element tr : trs) {
            match = new Match();
            i = 0;

            Elements names = tr.select("td.name div");
            if(names.size() < 1) {
                names = tr.select("td.today-name div");
            }
            match.setMember1(names.get(0).text());
            match.setMember2(names.get(1).text());

            Elements facs = tr.select(".js-price span");
            match.addFactor(Factor.WIN1, Utils.tryParseFloat(facs.get(i++).text()));
            match.addFactor(Factor.DRAW, Utils.tryParseFloat(facs.get(i++).text()));
            match.addFactor(Factor.WIN2, Utils.tryParseFloat(facs.get(i).text()));

            matches.add(match);
        }

        return matches;
    }
}